package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cjgl.entity.Course;
import cjgl.entity.Grade;
import cjgl.entity.Student;
import cjgl.entity.TbClass;
import service.CourseService;
import service.GradeService;
import service.StudentService;
import service.TbClassService;

/**
 * Servlet implementation class GradeServlet
 */
@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private TbClassService classService=new TbClassService();
	private StudentService studentService=new StudentService();
	private GradeService gradeService=new GradeService();
	private CourseService courseService=new CourseService();
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			peasonGrade(request, response);
		}else if("getStu".contentEquals(method)) {
			getStu(request, response);
		}else if("getPeasonGrade".contentEquals(method)) {
			getPeasonGrade(request,response);
		}else if("cjChange".contentEquals(method)) {
			cjChange(request,response);
		}else if("classGrade".contentEquals(method)) {
			classGrade(request,response);
		}else if("getClassGrade".contentEquals(method)) {
			getClassGrade(request,response);
		}else if ("allStudentGrade".contentEquals(method)) {
			allStudentGrade(request,response);
		}else if ("bjSel".contentEquals(method)) {
			bjSel(request,response);
		}else if ("selfGrade".contentEquals(method)) {
			selfGrade(request,response);
		}else if ("bjcj".contentEquals(method)) {
			bjcj(request,response);
		}
		}

	private void bjcj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student self =(Student)session.getAttribute("user");
		Integer bjId =self.getTbclass().getId();
		List<Course> courses = gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId, courses);
		request.setAttribute("kcs", courses);
		request.setAttribute("cjb", cjb);
		request.getRequestDispatcher("jsp/grade/cjb.jsp").forward(request, response);
		System.out.println(cjb);
	}

	private void selfGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student self =(Student)session.getAttribute("user");
		
		List<Grade> list10 = gradeService.findByStudent(self.getId());
		request.setAttribute("list10", list10);
		request.getRequestDispatcher("jsp/grade/selfGrade.jsp").forward(request, response);
		
	}

	private void bjSel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId = Integer.valueOf(request.getParameter("bjId"));
		List<Course> courses = gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId, courses);
		request.setAttribute("kcs", courses);
		request.setAttribute("cjb", cjb);
		request.getRequestDispatcher("jsp/grade/cjb.jsp").forward(request, response);
		System.out.println(cjb);
		
	}

	private void allStudentGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> list = classService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/allStudentGrade.jsp").forward(request, response);
		
	}

	private void getClassGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Integer bjId=Integer.valueOf(request.getParameter("bjId"));
		Integer kcId=Integer.valueOf(request.getParameter("kcId"));
		List<Student> students = studentService.findByClassId(bjId);
		List<Grade> grades=new ArrayList<Grade>();
		for(Student student : students) {
			Grade grade=gradeService.findByStudentIdAndCourseId(student.getId(),kcId);
			if(grade==null) {
				grade=new Grade(null,studentService.finById(student.getId()),courseService.findById(kcId),null);
			    gradeService.sava(grade);
			    
			}grades.add(grade);
		}
		request.setAttribute("grades", grades);
		request.getRequestDispatcher("jsp/grade/getClassGrade.jsp").forward(request, response);
		System.out.println(grades);
	}
	private void classGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<TbClass> bjs=classService.findAll();
		List<Course> course=courseService.findAll();
		request.setAttribute("bjs", bjs);
		request.setAttribute("kcs", course);
		request.getRequestDispatcher("jsp/grade/classGrade.jsp").forward(request, response);
		
	}
	private void cjChange(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		Integer id=Integer.valueOf(request.getParameter("id"));
		Integer cj=Integer.valueOf(request.getParameter("grade"));
		Grade grade=gradeService.findById(id);
		grade.setGrade(cj);
		gradeService.sava(grade);
		PrintWriter out = response.getWriter();
		out.print("ok");
		out.close();
	}
	private void getPeasonGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Integer stuId=Integer.valueOf(request.getParameter("stuId"));
		List<Grade>list3=gradeService.findByStudent(stuId);
		request.setAttribute("list3", list3);
		request.getRequestDispatcher("jsp/grade/getpeasonGrede.jsp").forward(request, response);
	}
	private void getStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   
		Integer bjId=Integer.valueOf(request.getParameter("bjId"));
		List<Student>list=studentService.findByClassId(bjId);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/getStu.jsp").forward(request, response);
		System.out.println(list);
	}
	private void peasonGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass>list1=classService.findAll();
		request.setAttribute("list1", list1);
		request.getRequestDispatcher("jsp/grade/peasonGereade.jsp").forward(request, response);
	   System.out.println(list1);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}
